BUI.use(['bui/overlay','bui/slider','bui/menu','bui/calendar','bui/tooltip'],function(Overlay,Slider,Menu,Calendar,Tooltip){
	function dialog(msg){
		 msg="操作出错："+msg;
         BUI.Message.Show({
                "msg" : msg,
				"mask":false,
                "icon" : 'question',
                "buttons" : [
                  {
                    text:'知道了',
                    elCls : 'button button-primary',
                    handler : function(){
                      this.close();
                    }
                  }
                ]
              });
	}	
	function login_dialog(msg){
		 msg="操作出错：登录超时。<h1>您必须重新登录才可以继续进行操作</h1>";
         BUI.Message.Show({
                "msg" : msg,
                "icon" : 'question',
				"mask":false,
                "buttons" : [{
                    text:'重新登录',
                    elCls : 'button ',
                    handler : function(){
					  window.location="http://container.api.taobao.com/container?appkey=21127968&scope=r1,r2,w1,w2";
                      this.close();
                    }
                  }
       
                ]
              });
	}
	var datepicker = new Calendar.DatePicker({
		trigger:'.calendar',
		showTime:true,
		autoRender : true
	});
	var adv_dialog = new Overlay.Dialog({
		title:'请添加订单内指定包含的商品',
		width:500,
		height:200,
		mask:false,
		success:function () {
		  this.close();
		},
		contentId:"adv_dialog",
		buttons:[
                  {
                    text:'确定',
                    elCls : 'button button-primary',
                    handler : function(){
						var val = ($(".value").val());
						$.ajax(
						{"url":"/?m=umpadmin&a=save_adv_rule&c=eventcfg",	
							"data":{"id":adv_items_id,"value":val,"adv_tp":adv_tp},
							"data":{"id":adv_items_id,"value":val,"adv_tp":adv_tp},
							"dataType": "json",
							"success":function(data,status){
								if(data.issuccess){
									if(val>0){
										switch(adv_tp){
											case "vip_user" :
												$("."+adv_tp+"_"+adv_items_id).text("必须是3个月内购买"+val+"次的老用户");
											break;
											case "sum_pay" :
											    $("."+adv_tp+"_"+adv_items_id).text("3个月内累计购买"+val+"元的老用户");
											break;
										}
									}else{
										switch(adv_tp){
											case "vip_user" :
												$("."+adv_tp+"_"+adv_items_id).text("不是老用户也可获得该折扣");
											break;
											case "sum_pay" :
											    $("."+adv_tp+"_"+adv_items_id).text("无需累计购买金额即可获得该折扣");
											break;
										}
									}
									adv_dialog.close();
								}else{
									dialog("保存错误，您没有输入新的数据，请重新输入");
								}
							}
						});
					}
				  }
				]
	});
	var adv_date_dialog = new Overlay.Dialog({
		title:'请添加订单内指定包含的商品',
		width:500,
		height:200,
		mask:false,
		success:function () {
		  this.close();
		},
		contentId:"adv_date",
		buttons:[
                  {
                    text:'确定',
                    elCls : 'button button-primary',
                    handler : function(){
						var start_time = $(".start_time").val();
						var end_time = $(".end_time").val();
						var decrease_amount = $(".decrease_amount").val();
						var friend_num = $(".friend_num").val();
						$.ajax(
						{"url":"/?m=umpadmin&a=save_adv_rule&c=eventcfg",	
							"data":{"id":adv_items_id,"eid":adv_items_eid,"friend_num":friend_num,"decrease_amount":decrease_amount*100,"tag":tag,"event_name":event_name,"start_time":start_time,"end_time":end_time,"adv_tp":adv_tp,"set_event_time":set_event_time,"set_cat_time":set_cat_time},
							"dataType": "json",
							"success":function(data,status){
								if(data.issuccess){
									if(!set_event_time && !set_cat_time){
										var text = "";
										if(start_time){
											text = "在"+start_time+"之后";
										}else{
											text = "3个月内";
										}
										if(end_time){
											text += "在"+end_time+"之前";
										}
										text+="有过购买的老用户";
										$("."+adv_tp+"_"+adv_items_id).text(text);
									}else{
										location.reload(true);
									}
									adv_date_dialog.close();
								}else{
									if(data.resp.code && data.resp.code==53){
										login_dialog();
										return;
									}
									var msg = "";
									if(data.resp){
										msg = data.resp.msg+data.resp.sub_msg;
									}else{
										msg = "保存错误，您没有输入新的数据，请重新输入";
									}
									dialog(msg);
								}
							}
						});
					}
				  }
				]
	});
	var adv_tp;
	$(".adv_rule").on("click",function(){
		adv_tp = $(this).attr("data-tp");
		adv_items_id = $(this).attr("data-id");
		var default_val = $(this).attr("data-def");
		$(".value").val("");
		$(".value").val(default_val);
		$(".start_time").val($(this).attr("data-start_time"));
		$(".end_time").val($(this).attr("data-end_time"));
		$(".msg-tip").show();
		$(".msg-info").hide();
		set_event_time = false;
		switch(adv_tp){
			case "vip_user" :
				$(".pre-msg").text("3个月内购买过");
				$(".aft-msg").text("次的老用户");
				adv_dialog.set("title","请设定老用户购买次数");
				adv_dialog.show();
			break;
			case "sum_pay" :
				$(".pre-msg").text("3个月累计购买");
				$(".aft-msg").text("元的老用户");
				adv_dialog.set("title","请设定总购买金额");
				adv_dialog.show();
			break;
			case "pay_time" :
				adv_date_dialog.set("title","指定时间段内有订单");
				adv_date_dialog.show();
			break;
		}
	});
	//高级条件中的商品
	var adv_items_eid;
	var adv_items_id;
	var adv_items = new Overlay.Dialog({
		title:'请添加订单内指定包含的商品',
		width:990,
		height:600,
		mask:false,
		success:function () {
		  this.close();
		},
		contentId:"adv_items",
		buttons:[
                  {
                    text:'确定',
                    elCls : 'button button-primary',
                    handler : function(){
						var iids = ($(".item-set-val").val());
						$.ajax(
						{"url":"/?m=umpadmin&a=save_adv_items&c=eventcfg",	
							"data":{"eid":adv_items_id,"iids":iids},
							"dataType": "json",
							"success":function(data,status){
								if(data.issuccess){
									if(iids.length>0){
										// $(".adv-item-count"+adv_items_eid).text(iids.split(",").length);
										$(".inf_"+adv_items_id).html("3个月任意订单内包含<span class='adv-item-count"+adv_items_eid+"'>"+iids.split(",").length+"</span>款指定商品；");
										$(".chk_"+adv_items_id).text("查看");
									}else{
										$(".inf_"+adv_items_id).text("无需指定购买商品即可获得该折扣");
										$(".chk_"+adv_items_id).text("增加订单包含商品");
									}
									adv_items.close();
								}else{
									alert("更新出错，请重试！");
								}
							}
						});
					}
				  }
				]
	});
	$(".check_iids").on("click",function(){
		var eid = $(this).attr("data-eid");
		var id = $(this).attr("data-id");
		adv_items_eid = eid;
		adv_items_id = id;
		$(".js-added").empty();
		$(".item-num").empty();
		$(".items_show").empty();
		$(".item-set-val").val("");
		$.ajax(
		{"url":"/?m=umpadmin&a=load_adv_items&c=eventcfg",	
			"data":{"eid":eid,"id":id},
			"dataType": "json",
			"success":function(data,status){
				$(".js-ev-"+eid).attr("data-ok","true");
				if(data.error){
					alert(data.error);
					return;
				}
				if(data.allcount>0){
					var html="";
					var iids=new Array();
					for(var i=0;i<data.promotion_range_list.promotion_range.length;i++){
						html+='<li class="item adv-item'+eid+'_'+data.promotion_range_list.promotion_range[i].item_id+'">';
						html+='<dl><img src="'+data.promotion_range_list.promotion_range[i].pic_url+'_120x120"></dl>';
						html+='<ol>'+data.promotion_range_list.promotion_range[i].item_title+'</ol>';
						html+='<ol><button class="button buttom-mini button-primary js-rm-adv-item" data-eid="'+eid+'" data-iid="'+data.promotion_range_list.promotion_range[i].item_id+'" type="button">删除</button></ol>';
						html+='</li>';
						iids.push(data.promotion_range_list.promotion_range[i].item_id);
					}
					$(".item-set-val").val(iids.join(","));
					$(".adv_items_show").empty();
					$(html).appendTo(".adv_items_show");
					$(".js-rm-adv-item").on("click",function(){
						var eid = $(this).attr("data-eid");
						var iid = $(this).attr("data-iid");
						var $iids = $(".item-set-val").val();
						var arr = $iids.split(",");
							arr.remove(iid);
							$iids = arr.join(",");
						$(".adv-item-count"+eid).text(arr.length);
						$(".adv-item"+eid+"_"+iid).remove();
						$(".item-set-val").val($iids);
					});
					adv_items.show();
				}else{
					$(".adv_items_show").empty();
					adv_items.show();
				}
				
			}
		});
	});	//高级条件中的商品
	
	$(".js-event").on("click",function(){
		var eid = $(this).attr("data-eid");
		var isok = $(this).attr("data-ok");
		if(!isok){
		$.ajax(
		{"url":"/?m=umpadmin&a=load_activ_items",	
			"data":{"id":eid},
			"dataType": "json",
			"success":function(data,status){
				$(".js-ev-"+eid).attr("data-ok","true");
				if(data.promotion_range_list.promotion_range){
					var html="";
					var iids=new Array();
					for(var i=0;i<data.promotion_range_list.promotion_range.length;i++){
						html+='<li class="item item'+eid+'_'+data.promotion_range_list.promotion_range[i].item_id+'">';
						html+='<dl><img src="'+data.promotion_range_list.promotion_range[i].pic_url+'_120x120"></dl>';
						html+='<ol>'+data.promotion_range_list.promotion_range[i].item_title+'</ol>';
						html+='<ol><button class="button buttom-mini button-primary js-rm-item" data-eid="'+eid+'" data-iid="'+data.promotion_range_list.promotion_range[i].item_id+'" type="button">删除</button></ol>';
						html+='</li>';
						iids.push(data.promotion_range_list.promotion_range[i].item_id);
					}
					$(".item-set-val"+eid).val(iids.join(","));
					$(".item_count"+eid).text("有"+data.item_count+"件打折商品");
					$(".items"+eid).empty();
					$(html).appendTo(".items"+eid);
					$(".js-rm-item").on("click",function(){
						var iid = $(this).attr("data-iid");
						var eid = $(this).attr("data-eid");
						$.ajax(
						{"url":"/?m=umpadmin&a=remove_activ_item",	
							"data":{"eid":eid,"iid":iid},
							"dataType": "json",
							"success":function(data,status){
								if(data.is_success){
									$(".item"+eid+"_"+iid).remove();
									var $iids = $(".item-set-val"+eid).val();
									var arr = $iids.split(",");
									    arr.remove(iid);
									$(".item-set-val"+eid).val(arr.join(","));
									$(".item_count"+eid).text("有"+arr.length+"件商品");
								}else{
									if(data.code=="53"){
										login_dialog(data.msg);
									}else if(data.code=="40"){
										dialog("您没有选择商品，请返回重新选择！");
									}else{
										dialog(data.msg);
									}
								}
							}
						});
					});
				}else{
					$(".items"+eid).empty();
					$(".item_count"+eid).text("没有任何商品！");
				}
			}
		});
		}
		if($(".js-icon-"+eid).hasClass("icon-chevron-up")){
			$(".js-content-"+eid).show();
			$(".js-icon-"+eid).addClass("icon-chevron-down");
			$(".js-icon-"+eid).removeClass("icon-chevron-up");
		}else{
			$(".js-content-"+eid).hide();
			$(".js-icon-"+eid).addClass("icon-chevron-up");
			$(".js-icon-"+eid).removeClass("icon-chevron-down");
		}
	});
	
	var glo_eid;
	var add_items = new Overlay.Dialog({
		title:'请添加或删除商品',
		width:990,
		height:600,
		mask:false,
		//配置DOM容器的编号
		success:function () {
		  this.close();
		},
		contentId:'content',
		"buttons" : [
                  {
                    text:'确定',
                    elCls : 'button button-primary',
                    handler : function(){
						var iids = ($(".item-set-val").val());
						var arr = iids.split(",");
						var old_iids = $(".item-set-val"+glo_eid).val();
						var old_arr = old_iids.split(",");
						for(var i=0;i<arr.length;i++){
							for(var j=0;j<old_arr.length;j++){
								if(arr[i]==old_arr[j]){
									arr.splice(i,1);
								}
							}
						}
						$.ajax(
						{"url":"/?m=umpadmin&a=add_activ_item",	
							"data":{"eid":glo_eid,"iids":arr.join(",")},
							"dataType": "json",
							"success":function(data,status){
								if(data.is_success){
									arr.concat(old_arr);
									$(".item-set-val"+glo_eid).val(arr.join(","));
									$(".item_count"+glo_eid).html("有"+arr.length+"件商品,<button class='button button-mini js-load' data-eid='"+glo_eid+"'>查看</button>");
									$(".js-load").on("click",function(){
										$(".js-ev-"+glo_eid).click();
										$(".js-ev-"+glo_eid).attr("data-ok","");
										$(".js-ev-"+glo_eid).click();
									});
								}else{
									if(data.code=="53"){
										login_dialog(data.msg);
									}else if(data.code=="40"){
										dialog("您没有选择商品，请返回重新选择！");
									}else{
										dialog(data.msg);
									}
								}
							}
						});
                      this.close();
                    }
                  },{
                    text:'取消',
                    elCls : 'button ',
                    handler : function(){
                      this.close();
                    }
                  }
		]
	});
	$(".js-add-items").on("click",function(){
		var eid = $(this).attr("data-eid");
		glo_eid = eid;
		$(".js-added").empty();
		$(".item-num").empty();
		$(".items_show").empty();
		$(".item-set-val").val("");
		$(".item-set-val").val($(".item-set-val"+eid).val());
		var html = $(".items"+eid).clone(true,true,true);
		$(html).prependTo(".items_show");
        add_items.show();
		add_items.set("y",$(document).scrollTop());
	});
	$(".js-load-items").on("click",function(){
		if($(this).attr("data-eitem")){
		$(".item-set-val").val("");
		$(".item-set-val").val($(".item-set-val"+glo_eid).val());
		}
	});
	var set_event_time = false;
	var event_name = "";
	var decrease_amount = "";
	var friend_num = "";
	var tag = "";
	$(".js-edit-time").on("click",function(){
		adv_tp = "pay_time";
		set_event_time = true;
		adv_items_id = $(this).attr("data-id");
		adv_items_eid = $(this).attr("data-eid");
		event_name = $(this).attr("data-name");
		decrease_amount = $(this).attr("data-decrease_amount");
		friend_num = $(this).attr("data-friend_num");
		tag = $(this).attr("data-tag");
		$(".start_time").val($(this).attr("data-start_time"));
		$(".end_time").val($(this).attr("data-end_time"));
		$(".decrease_amount").val(decrease_amount/100);
		$(".friend_num").val(friend_num);
		$(".msg-tip").hide();
		$(".msg-info").show();
		adv_date_dialog.set("title","设定活动时间");
		adv_date_dialog.show();
	});
	
	var set_cat_time = false;
	$(".set-cat-time").on("click",function(){
		set_cat_time=true;
		adv_items_id = $(this).attr("data-cat_id");
		$(".start_time").val($(this).attr("data-start_time"));
		$(".end_time").val($(this).attr("data-end_time"));
		$(".msg-tip").hide();
		$(".msg-info").hide();
		adv_date_dialog.set("title","设置参与活动的时间");
		adv_date_dialog.show();
	});
	$(".js-set-invite").on("click",function(){
		var prom_id = $(this).attr("data-promid");
		var ac_id = $(this).attr("data-eid");
		var cid = $(".cid").val();
	   BUI.Message.Show({
		"msg" : "是否设为被邀请人折扣？<br />被邀请的人立即获得该折扣，只能设置1个",
		"mask":false,
		"icon" : 'question',
		"buttons" : [
		  {
			text:'设为邀请折扣',
			elCls : 'button button-primary',
			handler : function(){
				var inv = $(".js-inv").attr("data-promid");
				if(inv){
					dialog("已经有别的被邀请人折扣，请先取消已有的被邀请人折扣。");
					return;
				}
				$.ajax(
					{"url":"/?m=umpadmin&c=eventcfg&a=set_invent_prom",	
						"data":{"prom_id":prom_id,"cid":cid},
						"dataType": "json",
						"success":function(data,status){
							if(data.is_success){
								$(".js-pan-"+ac_id).removeClass("panel-primary");
								$(".js-pan-"+ac_id).addClass("panel-danger");
								$(".js-pan-"+ac_id).addClass("js-inv");
								$(".invite-"+prom_id).addClass("button-danger");
								$(".invite-icon-"+prom_id).addClass("icon-white");
							}else{
								dialog(data.msg);
							}
						}
				});
			  this.close();
			}
		  },{
			text:'取消设置',
			elCls : 'button button-primary button-danger',
			handler : function(){
				$.ajax(
					{"url":"/?m=umpadmin&c=eventcfg&a=remove_invent_prom",	
						"data":{"prom_id":prom_id},
						"dataType": "json",
						"success":function(data,status){
							if(data.is_success){
								$(".js-pan-"+ac_id).removeClass("panel-danger");
								$(".js-pan-"+ac_id).addClass("panel-primary");
								$(".invite-"+prom_id).removeClass("button-danger");
								$(".invite-icon-"+prom_id).removeClass("icon-white");
							}else{
								dialog(data.msg);
							}
						}
				});
			  this.close();
			}
		  },{
			text:'取消',
			elCls : 'button ',
			handler : function(){
			  this.close();
			}
		  }
		]
	  });
	});
});